<template>
    <div>
        <form>
            <div class="form-group">
                <label for="comment">复制内容在Excel中粘贴：</label>
                <textarea
                    class="form-control"
                    rows="20"
                    style="resize:none;"
                    v-show="showTab"
                    v-model="contentTab"
                ></textarea>
                <textarea
                    class="form-control"
                    rows="20"
                    style="resize:none;"
                    v-show="!showTab"
                    v-model="contentCSV"
                ></textarea>
            </div>
            <p class="text-muted small">
                在
                <strong>文本框</strong>中按
                <mark>Ctrl+A</mark>、
                <mark>Ctrl+C</mark>，在
                <strong>Excel</strong>中按
                <mark>Ctrl+A</mark>设置格式为文本，选中
                <strong>A1</strong>单元格按
                <mark>Ctrl+V</mark>。
            </p>
        </form>
        <form>
            <button type="button" class="btn btn-dark" @click="getData()">导出</button>
            <span class="space-1em"></span>
            <label class="radio-inline" v-show="contentData" @click="showTab=true">
                <input type="radio" name="optradio" :checked="showTab" />Tab
            </label>
            <span class="space-1em"></span>
            <label class="radio-inline" v-show="contentData" @click="showTab=false">
                <input type="radio" name="optradio" :checked="!showTab" />CSV
            </label>
            <span class="space-1em"></span>
            <button
                type="button"
                class="btn btn-link"
                v-show="contentData"
                @click="showTable=true"
            >查看表格</button>
        </form>
        <tableView v-if="showTable&&contentData" :title="titleData" :source="contentData" @close="showTable=false"></tableView>
    </div>
</template>

<script>
import tableView from "./tableView.vue";
export default {
    name: "exportView",
    components: {
        tableView
    },
    data() {
        return {
            response: null,
            contentCSV: "",
            contentTab: "",
            contentData: null,
            titleData: null,
            showTab: true,
            showTable: false
        };
    },
    methods: {
        getData() {
            this.$store.commit("setLoading", true);
            this.axios
                .post("api/", {
                    action: "export",
                    payload: { login_data: this.$store.state.loginData }
                })
                .then(response => {
                    if (this.isDev) window.console.log(response.data);
                    this.response = response.data;
                    this.doExport();
                    this.$store.commit("setLoading", false);
                });
        },
        doExport() {
            let exportData = new Array();
            let basicData = this.response.content.basic_data;
            let extentionData = this.response.content.extention_data;
            let dataConfig = this.$store.state.dataConfig;
            let titleData = new Array();
            let calcRowNum = {
                row_age_person: -1,
                row_age_work: -1,
                row_age_work_all: -1
            };
            for (let i = 0; i < basicData.length; i++) {
                let row = new Array();
                let calcRes = {
                    age_work_before: 0,
                    time_start_work: "",
                    time_birth_date: ""
                };
                calcRes.time_birth_date = basicData[i].birth_date;
                for (let j = 0; j < dataConfig.length; j++) {
                    if (i == 0) {
                        // make title
                        let v = "";
                        if (dataConfig[j].is_vatal) {
                            v = "V";
                        }
                        titleData.push(v + dataConfig[j].title);
                        // get row of calc values
                        if (dataConfig[j].title == "年龄") {
                            calcRowNum.row_age_person = j;
                            window.console.log("年龄" + j);
                        } else if (dataConfig[j].title == "本单位工龄") {
                            calcRowNum.row_age_work = j;
                            window.console.log("本单位工龄" + j);
                        } else if (dataConfig[j].title == "总工龄") {
                            calcRowNum.row_age_work_all = j;
                            window.console.log("总工龄" + j);
                        }
                    }
                    let val = "";
                    if (dataConfig[j].is_basic) {
                        if (dataConfig[j].title == "编号") {
                            val = basicData[i].serial_number;
                        } else if (dataConfig[j].title == "姓名") {
                            val = basicData[i].clientName;
                        } else if (dataConfig[j].title == "人员工号") {
                            val = basicData[i].number;
                        } else if (dataConfig[j].title == "性别") {
                            val = basicData[i].sex;
                        } else if (dataConfig[j].title == "出生日期") {
                            val = basicData[i].birth_date;
                        } else if (dataConfig[j].title == "身份证号") {
                            val = basicData[i].id_number;
                        } else if (dataConfig[j].title == "民族") {
                            val = basicData[i].nation;
                        }
                    } else {
                        for (let k = 0; k < extentionData.length; k++) {
                            if (
                                calcRes.age_work_before==0 &&
                                extentionData[k].number ==
                                    basicData[i].number &&
                                extentionData[k].itemTitle == "入职前工龄"
                            ) {
                                calcRes.age_work_before = Number(
                                    extentionData[k].itemValue
                                );
                                window.console.log(
                                    "I=" +
                                        i +
                                        " 入职前工龄: " +
                                        calcRes.age_work_before
                                );
                            } else if (
                                calcRes.time_start_work=="" &&
                                extentionData[k].number ==
                                    basicData[i].number &&
                                extentionData[k].itemTitle == "入职时间"
                            ) {
                                calcRes.time_start_work =
                                    extentionData[k].itemValue;
                                window.console.log(
                                    "I=" +
                                        i +
                                        " 入职时间: " +
                                        calcRes.time_start_work
                                );
                            }
                            if (
                                !dataConfig[j].is_calc &&
                                extentionData[k].itemTitle ==
                                    dataConfig[j].title &&
                                extentionData[k].number ==
                                    basicData[i].number
                            ) {
                                val = extentionData[k].itemValue;
                                extentionData.splice(k, 1);
                            }
                        }
                    }
                    row.push(val);
                }
                // calc

                let age_person = this.FUNC.getAge(calcRes.time_birth_date);
                row[calcRowNum.row_age_person] = age_person;
                if (calcRes.time_start_work != "") {
                    let age_work = this.FUNC.getAge(calcRes.time_start_work);
                    let age_work_all = age_work + calcRes.age_work_before;
                    row[calcRowNum.row_age_work] = age_work;
                    row[calcRowNum.row_age_work_all] = age_work_all;
                    //
                    //window.console.log(calcRes.time_start_work+":"+age_work);
                }
                // calc fi
                exportData.push(row);
            }
            this.contentData = exportData;
            this.titleData = titleData;
            //new
            let exportTAB = titleData.join("\t");
            let exportCSV = titleData.join(",");
            for (let i = 0; i < exportData.length; i++) {
                exportTAB = exportTAB + "\n" + exportData[i].join("\t");
                exportCSV = exportCSV + "\n" + exportData[i].join(",");
            }
            this.contentTab = exportTAB;
            this.contentCSV = exportCSV;
        }
    }
};
</script>

<style scoped>
</style>